﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using Active.Core;
/// <summary>
/// AutoCompleteService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoCompleteService : System.Web.Services.WebService
{

    public AutoCompleteService()
    {
        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    /// <summary>
    /// Gets the media names list.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public String[] GetMediaNamesList(string prefixText, int count)
    {
        MediaList list = MediaList.GetMediaList(0, prefixText, 1, count);
        List<string> names = new List<string>(list.Count);
        foreach (MediaInfo info in list)
        {
            names.Add(info.Name);
        }

        return names.ToArray();
    }

    /// <summary>
    /// Gets the customer names list.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public String[] GetCustomerNamesList(string prefixText, int count)
    {
        CustomerList list = CustomerList.GetCustomerList(prefixText, 1, count);
        List<string> names = new List<string>(list.Count);
        foreach (CustomerInfo info in list)
        {
            names.Add(info.Name);
        }

        return names.ToArray();
    }

    [WebMethod]
    public String[] GetAgentNamesList(string prefixText, int count)
    {
        AgentList list = AgentList.GetAgentList(prefixText, 1, count);
        List<string> names = new List<string>(list.Count);
        foreach (AgentInfo info in list)
        {
            names.Add(info.Name);
        }

        return names.ToArray();
    }

    /// <summary>
    /// Gets the used positions.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public String[] GetUsedPositions(string prefixText, int count)
    {
        return MediaOrders.GetUsedPropertyList("Position", prefixText, count);
    }

    /// <summary>
    /// Gets the used time slots.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public String[] GetUsedTimeSlots(string prefixText, int count)
    {
        return MediaOrders.GetUsedPropertyList("TimeSlot", prefixText, count);
    }

    /// <summary>
    /// Gets the used durations.
    /// </summary>
    /// <param name="prefixText">The prefix text.</param>
    /// <param name="count">The count.</param>
    /// <returns></returns>
    [WebMethod]
    public String[] GetUsedDurations(string prefixText, int count)
    {
        return MediaOrders.GetUsedPropertyList("Duration", prefixText, count);
    }

    /// <summary>
    /// 从names中筛选出最匹配的WordList
    /// </summary>
    /// <param name="prefixText"></param>
    /// <param name="count"></param>
    /// <param name="names"></param>
    /// <returns></returns>
    private String[] GetWordList(string prefixText, int count, string[] names)
    {

        String[] autoCompleteWordList = names;

        Array.Sort(autoCompleteWordList, new CaseInsensitiveComparer());

        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());

        if (index < 0)
        {
            index = ~index;
        }

        int matchingCount;

        for (matchingCount = 0;matchingCount < count && index + matchingCount <autoCompleteWordList.Length;matchingCount++)
        {

            if (!autoCompleteWordList[index +matchingCount].StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase))
            {
                break;
            }
        }

        String[] returnValue = new string[matchingCount];

        if (matchingCount > 0)
        {
            Array.Copy(autoCompleteWordList, index, returnValue, 0,matchingCount);
        }

        return returnValue;
    }

}

